﻿using System;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;

namespace ToyStore.Infrastructure.NHibernateMaps.Conventions
{
    /// <summary>
    /// Konwencja dla enumów
    /// </summary>
    public class EnumConvention : IUserTypeConvention
    {
        /// <summary>
        /// Metoda sprawdzjąca czy typ jest Enumem
        /// </summary>
        /// <param name="criteria"></param>
        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
        {
            if (criteria == null)
                throw new ArgumentNullException("criteria");

            criteria.Expect(x => x.Property.PropertyType.IsEnum);
        }

        /// <summary>
        /// Motoda do zastosowania konwencji dla enumów
        /// </summary>
        /// <param name="instance"></param>
        public void Apply(IPropertyInstance instance)
        {
            if (instance == null)
                throw new ArgumentNullException("instance");

            instance.CustomType(instance.Property.PropertyType);
            instance.Index(string.Format("Idx_{0}_{1}", instance.EntityType.Name.InflectTo().Pluralized, instance.Property.Name));
        }
    }
}
